package com.zhihuichongcheng.application.ui.cricle.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.util.DeviceUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.cricle.CricleImageInfo;
import com.zhihuichongcheng.application.ui.cricle.activity.ShowImageActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/10/31.
 */

public class CricleDeatilImageAdapter extends RecyclerView.Adapter<CricleDeatilImageAdapter.ViewHold> {
    private Context mContext;
    private View.OnClickListener onClickListener;
    private List<CricleImageInfo> mList = new ArrayList<>();
    private int width =0;
    public CricleDeatilImageAdapter(Context context,List<CricleImageInfo> list)
    {
        this.mContext = context;
        this.mList = list;
        width = DeviceUtil.getWidth(mContext);
        width = width-DeviceUtil.dip2px(mContext,65);
    }
    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_add_image,null);
        return new ViewHold(view);
    }

    @Override
    public void onBindViewHolder(ViewHold holder, final int position) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width/3-DeviceUtil.dip2px(mContext,5),width/3-DeviceUtil.dip2px(mContext,5));
        params.setMargins(0,0,DeviceUtil.dip2px(mContext,5),DeviceUtil.dip2px(mContext,5));
        holder.mImAdd.setLayoutParams(params);
            Glide.with(mContext).load(mList.get(position).getJinbaoHomeImgUrl()).placeholder(R.drawable.ic_cricle_defalut_avatar).into(holder.mImAdd);

        holder.mImAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ShowImageActivity.class);
                intent.putParcelableArrayListExtra("list",(ArrayList) mList);
                intent.putExtra("index",position);
                mContext.startActivity(intent);
                ((Activity)mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setData(List<CricleImageInfo> list) {
        this.mList = list;

    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    class ViewHold extends RecyclerView.ViewHolder{
        private View view;
        private ImageView mImAdd;
        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mImAdd = (ImageView)itemView.findViewById(R.id.im_image);
        }
    }

}
